package com.itheima.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.*;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.metrics.ApplicationStartup;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

@Component
public class SpringContextHolder implements ApplicationContextAware, BeanFactoryAware, ApplicationEventPublisherAware, ApplicationStartupAware, EmbeddedValueResolverAware, EnvironmentAware, MessageSourceAware, ResourceLoaderAware {
    private static ApplicationContext applicationContext;

    private static BeanFactory beanFactory;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("<<<<<< SpringContextHolder.applicationContext = applicationContext");
        SpringContextHolder.applicationContext = applicationContext;
    }


    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }


    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("<<<<<< SpringContextHolder.beanFactory = beanFactory");
        SpringContextHolder.beanFactory = beanFactory;
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        System.out.println("<<<<<< SpringContextHolder.applicationEventPublisher = applicationEventPublisher");
    }

    @Override
    public void setApplicationStartup(ApplicationStartup applicationStartup) {
        System.out.println("<<<<<< SpringContextHolder.applicationStartup = applicationStartup");
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        System.out.println("<<<<<< SpringContextHolder.resolver = resolver");
    }

    @Override
    public void setEnvironment(Environment environment) {
        System.out.println("<<<<<< SpringContextHolder.environment = environment");
    }

    @Override
    public void setMessageSource(MessageSource messageSource) {
        System.out.println("<<<<<< SpringContextHolder.messageSource = messageSource");
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        System.out.println("<<<<<< SpringContextHolder.resourceLoader = resourceLoader");
    }
}
